# -*- coding: utf-8 -*-
"""
@Project ：PMS_AUTO_TEST 
@File    ：fun_update_phone.py
@Author  ：罗昕晖
@Date    ：2021/5/13 9:45
"""
import time
import allure
from time import sleep
from common.utils import Utils
from config.config import Config
from dz_service.page_elements.el_update_phone import update_phone
from dz_service.page_elements.el_common import common
from dz_service.page_elements.el_home import home
from dz_service.function.fun_common import Common
from common.page import Page
from dz_service.function.fun_service_utils import ServiceUtils


class UpdatePhone:

    @allure.step('step:作废网厅新中台、BOP业务流水')
    def initial_userinfo(self, client_id):
        ServiceUtils().initial_service_sql(client_id, '1061')
        ServiceUtils().initial_bop_sql(client_id, '1001')

    def create_phone(self):
        datetime = time.strftime("%m%d%H%M%S", time.localtime())
        phone = f"1{datetime}"
        return phone

    @allure.step('step:业务办理前置检查')
    def check_mobile(self):
        '''

        :return:
        '''
        Page.click(home, '首页', '手机号码修改')
        # 完整性检查页
        ServiceUtils.assert_element([(update_phone, '检查元素', '资料完整', '资料完整', '资料完整性校验失败。'),
                               (update_phone, '检查元素', '风险测评未过期', '风险测评未过期', '风险测评未过期校验失败。'),
                               (update_phone, '检查元素', '证件日期未过期', '证件日期未过期', '证件日期未过期校验失败。')], log='完整性检查效验成功。')
        # 点击菜单--下一步
        Common().click_next()

        # 业务说明页
        ServiceUtils.assert_element([(update_phone, '检查元素', '手机号修改', '手机号修改', '打开业务说明页失败。')], log='打开业务说明页成功。')
        # 点击菜单--立即办理
        Common().click_handle_now()

        # 准入性检查页
        ServiceUtils.assert_element([(common, '公共页面', '个人户', '个人户', '打开账户准入性检查失败。')], log='打开账户准入性检查成功。')
        # ServiceUtils.assert_element([(common, '公共页面', '您符合名单办理要求', '您符合名单办理要求', '您不符合名单办理要求。')], log='您符合名单办理要求。') #生产有、测试没
        # 点击菜单--下一步
        Common().click_next()
        Page.check_page_loading(10)

    @allure.step('step:验证码更新手机号')
    def update_phone(self, message_code, phone):
        # 原手机号验证页
        Page.click(update_phone, '手机号更新', '获取验证码')
        Page.send_key(update_phone, '手机号更新', '输入验证码', message_code)
        Page.check_page_loading(10)
        # 点击菜单--下一步
        Common().click_next()

        # 手机号验证页
        Page.check_page_loading(10)
        Page.send_key(update_phone, '手机号更新', '输入新手机号', phone)
        Page.click(update_phone, '手机号更新', '获取验证码')
        Page.send_key(update_phone, '手机号更新', '输入验证码', message_code)
        # 点击菜单--提交
        Page.click(common, '公共页面', '提交')
        ServiceUtils.assert_element([(update_phone, '检查元素', '提交成功', '提交成功', '提交失败。')], log='提交成功。')

    @allure.step('step:证件更新手机号')
    def update_phone_for_IDcard(self, positive_pic, back_pic, phone, message_code):
        positive_pic_path = Utils.get_test_pic(positive_pic)
        back_pic_path = Utils.get_test_pic(back_pic)

        Page.click(update_phone, '手机号更新', '原手机号无法使用')
        Page.check_page_loading(10)
        sleep(0.5)
        # 上传证件
        Common().upload_id_card_pic(positive_pic_path, back_pic_path)

        # 手机号验证页
        Page.check_page_loading(10)
        Page.send_key(update_phone, '手机号更新', '输入新手机号', phone)
        Page.click(update_phone, '手机号更新', '获取验证码')
        Page.send_key(update_phone, '手机号更新', '输入验证码', message_code)

        # 点击菜单--提交
        Page.click(common, '公共页面', '提交')
        ServiceUtils.assert_element([(update_phone, '检查元素', '提交成功', '提交成功', '提交失败。')], log='提交成功。')

    @allure.step('step:检查大字段表中身份证更新数据')
    def check_update_phone(self, client_id, phone):
        if Config().env == 'test':
            search_result = Utils.search_client(client_id)
            Page.logger.info(search_result, extra={'appname': 'check_update_phone'})
            Utils.check_val(client_id, search_result['clientId'], "客户号")
            Utils.check_val(phone, search_result['newMobileTel'], "新手机号码")
        else:
            assert True
